<template>
  <a-modal
      title="编辑用户"
      :width="600"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
                label="用户名"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-decorator="['loginName', {initialValue: user.loginName, rules: [
                  { required: true, message: '请输入用户名', trigger: 'blur' },
                  { type: 'string', min: 2, message: '用户名不允许小于2位', trigger: 'blur' }]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
                label="昵称"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['userName', {initialValue: user.userName}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
                label="用户手机"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-decorator="['userPhone', {initialValue: user.userPhone, rules: [
                      { validator: validatePhone, trigger: 'blur' }]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
                label="身份证号"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-decorator="['idCard', {initialValue: user.idCard, rules: [
                      { validator: validateCard, trigger: 'blur' }]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {editUser} from '@/api/basic/user'

export default {
  props: {},
  data () {
    return {
      labelCol: {
        xs: {span: 24},
        sm: {span: 7}
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16}
      },
      visible: false,
      confirmLoading: false,
      user: {},

      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit (record) {
      this.visible = true
      this.user = record
    },
    // 前端验证手机号码
    validatePhone (rule, value, callback) {
      const reg = /^[1][3-9][0-9]{9}$/
      if (value === '' || value === undefined || value === null) {
        callback()
      } else {
        if ((!reg.test(value)) && value !== '') {
          callback(new Error('请输入正确的电话号码'))
        } else {
          callback()
        }
      }
    },
    // 前端验证身份证号
    validateCard (rule, value, callback) {
      const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
      if (value === '' || value === undefined || value === null) {
        callback()
      } else {
        if (!reg.test(value)) {
          callback(new Error('请输入正确的身份证号码'))
        } else {
          callback()
        }
      }
    },
    handleSubmit () {
      const {form: {validateFields}} = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          editUser(Object.assign(values, {'userId': this.user.userId}))
              .then(() => {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              }).catch(err => {
            this.confirmLoading = false
            this.$message.error(err.result || '操作失败！')
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
